package com.tools.cleanmaster.scenenew;

import android.content.Context;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.functionpage.FunctionItemSharedPrefs;
import com.tools.cleanmaster.manager.NotificationSceneManager;
import com.tools.cleanmaster.store.SceneSharedPrefManager;
import com.tools.cleanmaster.ui.notification.item.BatteryLowItem;
import com.tools.cleanmaster.ui.notification.item.BatterySharpDecItem;
import com.tools.cleanmaster.utils.FeatureConfig;
import com.tools.cleanmaster.utils.LogHelper;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 电量变化场景
 */
public class SceneBatteryChange implements BatteryInfoTracker.BatteryInfoListener {
    public static final String TAG = "Scene";
    private Context mContext;
    private int mLastBatteryPercent;

    private SceneBatteryChange() {
    }

    private static SceneBatteryChange sInstance;

    public static SceneBatteryChange getInstance() {
        if (sInstance == null) {
            synchronized (SceneBatteryChange.class) {
                if (sInstance == null) {
                    sInstance = new SceneBatteryChange();
                    sInstance.init();
                }
            }
        }
        return sInstance;
    }

    private void init() {
        mContext = CleanApplication.getInstance();
    }

    @Override
    public void onBatteryInfoUpdated(BatteryInfoTracker.BatteryInfo newData) {
        if (mLastBatteryPercent == newData.percent) {
            if (FeatureConfig.DEBUG_LOG) {
                LogHelper.d(TAG, "电量百分比和上回变化一致，不作场景化检测");
            }
            return;
        }
        mLastBatteryPercent = newData.percent;
        batteryLow(newData);        // 低电量场景检测
        batterySharpDec(newData);   // 快速耗电场景检测
        FunctionItemSharedPrefs.batterySharpDec(newData);
    }

    /**
     * 开始场景
     */
    public void start() {
        BatteryInfoTracker.getInsance(mContext).registerListener(this);
    }


    private void batteryLow(BatteryInfoTracker.BatteryInfo newData) {
        String jsonBody = SceneSharedPrefManager.getSingleSceneConfigData(SceneType.BATTERY_LOW);
        int thresholdPrecent = BatteryLowItem.DEFAULT_PERCENT;
        try {
            JSONObject jsonObject = new JSONObject(jsonBody);
            thresholdPrecent = jsonObject.optInt(BatteryLowItem.JSON_KEY_PERCENT, BatteryLowItem.DEFAULT_PERCENT);
        } catch (JSONException e) {
            if (FeatureConfig.DEBUG_LOG) {
                e.printStackTrace();
            }
        }

        int percent = newData.percent;
        if (percent <= thresholdPrecent) {
            if (FeatureConfig.DEBUG_LOG) {
                LogHelper.d(TAG, "电量" + percent + ",开始判断低电量场景化");
            }
            if (NotificationSceneManager.getInstance().checkAndRestTimeLimit()) {
                BatteryLowItem item = new BatteryLowItem();
                if (item.checkRules()) {
                    NotificationSceneManager.getInstance().showSceneNotification(item);
                }
            }
        } else if (FeatureConfig.DEBUG_LOG) {
            LogHelper.d(TAG, "电量" + percent + " 大于" + thresholdPrecent + ",无法展示");
        }
    }

    private void batterySharpDec(BatteryInfoTracker.BatteryInfo newData) {
        boolean isAplus = SceneType.BAT_SHARPDEC == SceneToolkit.getCurrentAplusScene(mContext);
        if (isAplus || (!isAplus && NotificationSceneManager.getInstance().checkAndRestTimeLimit())) {
            boolean isValid = BatterySharpDecHelper.getInstance().batterySharpDec(newData);
            if (isValid) {
                BatterySharpDecItem item = new BatterySharpDecItem();
                if (item.checkRules()) {
                    NotificationSceneManager.getInstance().showSceneNotification(item);
                }
            }
        }
    }
}
